package com.company.oa.hr.service.recruitment.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.oa.common.exception.BusinessException;
import com.company.oa.hr.dto.recruitment.PositionDTO;
import com.company.oa.hr.entity.recruitment.Position;
import com.company.oa.hr.mapper.recruitment.PositionMapper;
import com.company.oa.hr.service.recruitment.PositionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService {

    @Override
    public IPage<Position> getPositionPage(int page, int size, Position position) {
        Page<Position> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<Position> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        queryWrapper.like(StringUtils.hasText(position.getPositionName()), Position::getPositionName, position.getPositionName())
                   .eq(position.getDepartmentId() != null, Position::getDepartmentId, position.getDepartmentId())
                   .eq(position.getStatus() != null, Position::getStatus, position.getStatus())
                   .orderByDesc(Position::getCreateTime);
        
        return page(pageParam, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createPosition(PositionDTO positionDTO, Long creatorId, String creatorName) {
        Position position = new Position();
        BeanUtils.copyProperties(positionDTO, position);
        
        // 设置状态为草稿
        position.setStatus(0);
        
        // 设置创建信息
        position.setCreatorId(creatorId);
        position.setCreatorName(creatorName);
        position.setCreateTime(new Date());
        
        return save(position);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePosition(PositionDTO positionDTO) {
        // 检查职位是否存在
        Position existPosition = getById(positionDTO.getId());
        if (existPosition == null) {
            throw new BusinessException("职位不存在");
        }
        
        // 已发布的职位不能修改基本信息
        if (existPosition.getStatus() != 0 && existPosition.getStatus() != 3) {
            throw new BusinessException("已发布的职位不能修改基本信息");
        }
        
        Position position = new Position();
        BeanUtils.copyProperties(positionDTO, position);
        
        // 设置更新时间
        position.setUpdateTime(new Date());
        
        return updateById(position);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletePosition(Long id) {
        // 检查职位是否存在
        Position existPosition = getById(id);
        if (existPosition == null) {
            throw new BusinessException("职位不存在");
        }
        
        // 已发布的职位不能删除
        if (existPosition.getStatus() != 0 && existPosition.getStatus() != 3) {
            throw new BusinessException("已发布的职位不能删除");
        }
        
        return removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean publishPosition(Long id) {
        // 检查职位是否存在
        Position existPosition = getById(id);
        if (existPosition == null) {
            throw new BusinessException("职位不存在");
        }
        
        // 只有草稿或暂停状态的职位可以发布
        if (existPosition.getStatus() != 0 && existPosition.getStatus() != 3) {
            throw new BusinessException("只有草稿或暂停状态的职位可以发布");
        }
        
        Position position = new Position();
        position.setId(id);
        position.setStatus(1); // 已发布状态
        position.setUpdateTime(new Date());
        
        return updateById(position);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean pausePosition(Long id) {
        // 检查职位是否存在
        Position existPosition = getById(id);
        if (existPosition == null) {
            throw new BusinessException("职位不存在");
        }
        
        // 只有已发布状态的职位可以暂停
        if (existPosition.getStatus() != 1) {
            throw new BusinessException("只有已发布状态的职位可以暂停");
        }
        
        Position position = new Position();
        position.setId(id);
        position.setStatus(3); // 已暂停状态
        position.setUpdateTime(new Date());
        
        return updateById(position);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean endPosition(Long id) {
        // 检查职位是否存在
        Position existPosition = getById(id);
        if (existPosition == null) {
            throw new BusinessException("职位不存在");
        }
        
        // 只有已发布或已暂停状态的职位可以结束
        if (existPosition.getStatus() != 1 && existPosition.getStatus() != 3) {
            throw new BusinessException("只有已发布或已暂停状态的职位可以结束");
        }
        
        Position position = new Position();
        position.setId(id);
        position.setStatus(2); // 已结束状态
        position.setUpdateTime(new Date());
        
        return updateById(position);
    }
} 